﻿using Furion.DatabaseAccessor.Extensions;
using Furion.LinqBuilder;
using Furion.Localization;
using ST.Torch.WebApi.Application.System.Dtos;
using ST.Torch.WebApi.Application.System.Services;
using ST.Torch.WebApi.Application.Utils.Services;
using ST.Torch.WebApi.Core.Entities.System;
using ST.Torch.WebApi.Core.Entities.Utils;
using ST.Torch.WebApi.Core.Manage.Cache.Dto;
using ST.Torch.WebApi.Core.Manager;
using ST.Torch.WebApi.Core.Utils;
using Mapster.Utils;
using MiniExcelLibs;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ST.Torch.WebApi.Application.System
{

    /// <summary>
    /// 系统服务接口
    /// </summary>
    [AppAuthorize]
    public class FormDataAppService : IDynamicApiController
	{
		private readonly IFormDataService _FormDataService;
		private readonly SYSMenuService _SYSMenuService;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ICacheManage _ICacheManage;
        

        public FormDataAppService(IFormDataService FormDataService, SYSMenuService sYSMenuService, IHttpContextAccessor httpContextAccessor, ICacheManage cacheManage)
        {
            _FormDataService = FormDataService;
            _SYSMenuService = sYSMenuService;
            _httpContextAccessor = httpContextAccessor;
                _ICacheManage = cacheManage;
        }


        [HttpPost]
		public async Task<PagedList<FormDataListDto>> GetDataPagedList([FromForm] PageReq req)
		{
			Expression<Func<FormData, bool>> predicate = u => true;
            predicate=predicate.AndIf(!string.IsNullOrEmpty(req.key),u=>u.Title.Contains(req.key));
			
			var dataList= await _FormDataService.QueryPageAsync(predicate, req);
			PagedList<FormDataListDto> resultList = dataList.Adapt<PagedList<FormDataListDto>>();

			return resultList;
		}
        [HttpPost]
        public async Task<FormDataListDto> Single([FromForm] Guid Id)
        {
           
           var entity= await _FormDataService.SingleAsync(u=>u.Id==Id);
            FormDataListDto resultData = entity.Adapt<FormDataListDto>();
			return resultData;

        }
        [HttpPost]
        public async Task<FormDataListDto> First()
        {

            var entity = await _FormDataService.SingleAsync(u => true);
            if (entity == null)
            {
                entity = new FormData();
            }
            FormDataListDto resultData = entity.Adapt<FormDataListDto>();
            return resultData;

        }
        [HttpPost]
        public async Task Insert([FromForm] FormDataDto model)
        {
         
                FormData entity = model.Adapt<FormData>();
                await _FormDataService.InsertAsync(entity);
            
        }
		[HttpPost]
		public async Task Update([FromForm] FormDataListDto model)
		{
			
			var entity=await _FormDataService.SingleAsync(u => u.Id == model.Id);
            if (entity == null)
            {
                 entity = model.Adapt<FormData>();
                await _FormDataService.InsertAsync(entity);
                return;
            }
			entity.Title=model.Title;
			entity.City=model.City;
			entity.Desc=model.Desc;
			entity.Sex=model.Sex;
			entity.Switch=model.Switch;
			entity.Like=model.Like;
            entity.DescTinymce=model.DescTinymce;
            entity.ImgUrl=model.ImgUrl;

            await _FormDataService.UpdateAsync(entity);
		}
		[HttpPost]
		public async Task DeleteAsync([FromForm] Guid Id)
		{
			var entity = await _FormDataService.SingleAsync(u => u.Id == Id);
			await _FormDataService.DeleteAsync(entity);
		}

        [HttpPost]
        public async Task DeleteBatchAsync([FromForm] List<Guid> Ids)
        {
            var entitys = await _FormDataService.QueryAsync(u => Ids.Contains(u.Id));
            foreach (var entity in entitys)
            {
                await _FormDataService.DeleteAsync(entity);
            }
        }



    }

}
